// live-list/live-list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        category: [],
        choiceList: [], // 精选顶部
        list: [],
        page: 1,
        loading: true,
        isStop: false,
        categoryLeft: 0,
        currentIndex: 0, // 精选index
        currentCategoryIndex: 'choice', // 分类
        systemInfo: null,
        listLeft: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        getApp().page.onLoad(this, options)
        this.setData({
            systemInfo: getApp().core.getSystemInfoSync()
        })
        this.getCat()
        this.getList()
    },

    /**
     * 获取当前分类
     */
    getCurrentCat: function () {
        if (this.data.currentCategoryIndex == 'follow' || this.data.currentCategoryIndex == 'choice') {
            return this.data.currentCategoryIndex
        }
        return this.data.category[this.data.currentCategoryIndex].id
    },

    /**
     * 获取列表
     */
    getList: function () {
        wx.showLoading({
            title: '加载中...',
        });
        this.setData({
            isStop: false
        })
        let currentCat = this.getCurrentCat()
        getApp().request({
            url: getApp().api.live.get_list,
            data: {
                cat_id: currentCat
            },
            success: data => {
                if (data.code == 0) {
                    this.setData({
                        list: data.data.list
                    })
                    if (currentCat == 'choice') {
                        this.setData({
                            choiceList: data.data.choice_list
                        })
                    }
                } else {
                    wx.showModal({
                        title: "提示",
                        content: data.msg,
                        showCancel: !1,
                        confirmText: "确认",
                        success: function (t) {
                            t.confirm && wx.navigateBack({
                                delta: 1
                            });
                        }
                    })
                }
            },
            complete: () => {
                this.setData({
                    loading: true
                });
                wx.hideLoading();
            }
        });
    },

    /**
     * 获取分类
     */
    getCat: function () {
        wx.showLoading({
            title: '加载中...',
        });
        getApp().request({
            url: getApp().api.live.get_cat,
            success: res => {
                if (res.code == 0) {
                    this.setData({
                        category: res.data.list
                    })
                }
            },
            complete: () => {
                wx.hideLoading()
            }
        })
    },

    /**
     * 点击直播
     */
    onClickLive: function (e) {
        let item = e.currentTarget.dataset.item
        let type = e.currentTarget.dataset.type
        if (type == 'anchor') {
            wx.navigateTo({
                url: '/lives/anchor/anchor?anchor_id=' + item.anchor_id
            })
            return
        }
        wx.navigateTo({
            url: '/lives/live/live?room_id=' + item.id
        })
    },

    /**
     * 点击分类
     */
    onClickCategory: function (e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            currentCategoryIndex: index,
            page: 1
        })
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.isStop) {
            return;
        }
        wx.showLoading({
            title: '加载中...',
        });
        getApp().request({
            url: getApp().api.live.get_list,
            data: {
                page: this.data.page + 1,
                cat_id: this.getCurrentCat()
            },
            success: data => {
                if (data.code == 0) {
                    if (data.data.list.length > 0) {
                        this.setData({
                            list: this.data.list.concat(data.data.list),
                            page: this.data.page + 1
                        });
                    } else {
                        this.setData({
                            isStop: true
                        });
                    }

                } else {
                    wx.showToast({
                        title: data.msg,
                        icon: 'none'
                    });
                }
            },
            complete: () => {
                wx.hideLoading();
            }
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

  },
  changeSwiper: function(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
})